﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using ARR.Shared;
using System.Globalization;
using ARR.Entities;
using ARR.UI.AttributesService;

namespace ARR.UI.Helpers
{
    public class ParetoConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Article item = value as Article;

            if (item != null)
            {
                if (!item.IsDeletedRangeReview)
                {
                    if (item.AcumSales <= 0.8m || item.PrevAcumSales < 0.8m)
                    {
                        return 1;
                    }
                    else if (item.AcumSales >= (decimal)(1 - (WorkspaceData.Instance.RangeReview.Bottom / 100)))
                    {
                        return 2;
                    }
                }
                else
                {
                    return 3;
                }
            }

            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class StoreSegmentationsEnumToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return (StoreSegmentations)Enum.Parse(typeof(StoreSegmentations), value.ToString(), true);
            }
            else
            {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return value;
            }
        }
    }

    public class BooleanToTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value) ? Labels.Si : Labels.No;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class MonthNumberToMonthName : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string monthName = null;

            switch ((int)value)
            {
                case 1: monthName = "Enero"; break;
                case 2: monthName = "Febrero"; break;
                case 3: monthName = "Marzo"; break;
                case 4: monthName = "Abril"; break;
                case 5: monthName = "Mayo"; break;
                case 6: monthName = "Junio"; break;
                case 7: monthName = "Julio"; break;
                case 8: monthName = "Agosto"; break;
                case 9: monthName = "Septiembre"; break;
                case 10: monthName = "Octubre"; break;
                case 11: monthName = "Noviembre"; break;
                case 12: monthName = "Diciembre"; break;
            }

            return monthName;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class NumberToRangeConverter : IValueConverter
    {
        public NumberToRangeConverter()
        {
            AttributesServiceClient service = new AttributesServiceClient();

            try
            {
                AttributeTypes = service.GetAllTypesWithValues().Where(x => x.IsArticleBaseColumn).ToList();
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public List<AttributeType> AttributeTypes { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string label = null;


            if (value != null)
            {
                AttributeType type = AttributeTypes.SingleOrDefault(x => x.Name == parameter.ToString());

                if (type != null)
                {
                    Entities.Attribute att = type.Values.Where(x => x.MaxValue.HasValue && x.MinValue.HasValue)
                                                        .SingleOrDefault(x => x.MinValue.Value <= (decimal)value && x.MaxValue >= (decimal)value);

                    if (att != null)
                    {
                        label = att.AttributeValue;
                    }
                    else
                    {
                        label = value.ToString();
                    }
                }
                else
                {
                    //No hay labels configurados para el atributo
                    label = value.ToString();
                }
            }
            else
            {
                label = "";
            }

            return label;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class ErrorTypeToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var type = (ErrorType)value;

                if (type == ErrorType.Error)
                {
                    return "/Images/Error-16.png";
                }
                if (type == ErrorType.Information)
                {
                    return "/Images/Information-16.png";
                }
                if (type == ErrorType.Warning)
                {
                    return "/Images/Warning-16.png";
                }
            }

            return null;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class AttributeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                int typeId = 0;

                if (parameter != null)
                    typeId = Int32.Parse(parameter.ToString());

                List<Entities.Attribute> attributes = value as List<Entities.Attribute>;

                var att = attributes.Where(p => p.AttributeTypeId == typeId).FirstOrDefault();
                if (att != null)
                {
                    return att.AttributeValue;
                }
                else
                {
                    return null;
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    public class SalesByDateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                DateTime date;

                if (parameter != null)
                {
                    date = DateTime.Parse(parameter.ToString());

                    Dictionary<DateTime, string> values = value as Dictionary<DateTime, string>;
                    return values.Where(x => x.Key == date).FirstOrDefault();
                }
                else
                {
                    return null;
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    public class SpacesByLocationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                if (parameter != null)
                {
                    Dictionary<string, string> values = value as Dictionary<string, string>;
                    return values.Where(x => x.Key == parameter.ToString()).FirstOrDefault();
                }
                else
                {
                    return null;
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
